package com.linrung.gdsfrz;

import java.security.MessageDigest;
import java.util.Date;

/**
 * 签名算法主要使用以下几个字段：
 x-tif-timestamp：当前时间 unix 时间戳，精确到秒
 x-tif-nonce：由调用者/被调用者/网关生成的非重复的随机字符串（十分钟内不能重复）
 PaaSToken：创建应用时分配的加密密钥；
 签名算法：
 x-tif-signature = sha256(x-tif-timestamp + PaaSToken + x-tif-nonce + x-tif-timestamp)
 * @author wurenlong
 */
public class Signature {

    private static final String DIGEST_TYPE = "SHA-256";

    private static final String ENCODING = "UTF-8";

    private String paasId;
    private String paasToken;
    private String signature;
    private String timestamp;
    private String nonce;

    public Signature(String paasId, String paasToken) throws Exception {
        super();
        long now = System.currentTimeMillis();
        this.paasId = paasId;
        this.paasToken = paasToken;
        this.timestamp = Long.toString((long) Math.floor(now / 1000));
        this.nonce = Long.toHexString(now) + "-" + Long.toHexString((long) Math.floor(Math.random() * 0xFFFFFF));
        this.signature = toSHA256(timestamp + paasToken + nonce + timestamp);
    }

    /**
     * 获取加密摘要
     * @param paasId
     * @param paasToken
     * @param timestamp
     * @param nonce
     * @return
     * @throws Exception
     */
    public static String messageDigest(String paasId, String paasToken, String timestamp, String nonce) throws Exception {
        return toSHA256(timestamp + paasToken + nonce + timestamp);
    }

    public static String toSHA256(String str) throws Exception {
        MessageDigest messageDigest;
        String encodeStr = "";
        try {
            messageDigest = MessageDigest.getInstance(DIGEST_TYPE);
            messageDigest.update(str.getBytes(ENCODING));
            encodeStr = byte2Hex(messageDigest.digest());
        } catch (Exception e) {
            throw e;
        }
        return encodeStr;
    }

    // byte转换成16进制
    private static String byte2Hex(byte[] bytes) {
        StringBuffer stringBuffer = new StringBuffer();
        String temp = null;
        for (int i = 0; i < bytes.length; i++) {
            temp = Integer.toHexString(bytes[i] & 0xFF);
            if (temp.length() == 1) {
                stringBuffer.append("0");
            }
            stringBuffer.append(temp);
        }
        return stringBuffer.toString();
    }

    public String getPaasId() {
        return paasId;
    }

    public void setPaasId(String paasId) {
        this.paasId = paasId;
    }

    public String getPaasToken() {
        return paasToken;
    }

    public void setPaasToken(String paasToken) {
        this.paasToken = paasToken;
    }

    public String getSignature() {
        return signature;
    }

    public void setSignature(String signature) {
        this.signature = signature;
    }

    public String getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(String timestamp) {
        this.timestamp = timestamp;
    }

    public String getNonce() {
        return nonce;
    }

    public void setNonce(String nonce) {
        this.nonce = nonce;
    }

}
